package com.yida.gwj.basis.service.impl;

import com.yida.common.core.text.Convert;
import com.yida.gwj.basis.domain.GwjInitBusinessHouse;
import com.yida.gwj.basis.domain.dto.GwjInitBusinessHouseDTO;
import com.yida.gwj.basis.mapper.GwjInitBusinessHouseMapper;
import com.yida.gwj.basis.service.IGwjInitBusinessHouseService;
import com.yida.gwj.common.enums.GwjBusinessHouseStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 业务房屋信息（基础数据管理模块）Service业务层处理
 *
 * @author lqq
 * @date 2022-11-15
 */
@Service
public class GwjInitBusinessHouseServiceImpl implements IGwjInitBusinessHouseService {

	@Resource
	private GwjInitBusinessHouseMapper businessHouseMapper;

	/**
	 * 新增业务房屋信息（基础数据管理模块）
	 *
	 * @param businessHouse 业务房屋信息（基础数据管理模块）
	 * @return 结果
	 */
	@Override
	public int save(GwjInitBusinessHouse businessHouse) {
		return businessHouseMapper.insert(businessHouse);
	}

	/**
	 * 批量新增业务房屋信息（基础数据管理模块）
	 *
	 * @param businessHouses 业务房屋信息（基础数据管理模块）
	 * @return 结果
	 */
	@Override
	public int saveBatch(List<GwjInitBusinessHouse> businessHouses) {
		return businessHouseMapper.insertBatch(businessHouses);
	}

	/**
	 * 修改业务房屋信息（基础数据管理模块）
	 *
	 * @param businessHouse 业务房屋信息（基础数据管理模块）
	 * @return 结果
	 */
	@Override
	public int updateById(GwjInitBusinessHouse businessHouse) {
		return businessHouseMapper.updateById(businessHouse);
	}

	/**
	 * 批量修改业务房屋信息（基础数据管理模块）
	 *
	 * @param businessHouses 业务房屋信息（基础数据管理模块）
	 * @return 结果
	 */
	@Override
	public int updateBatch(List<GwjInitBusinessHouse> businessHouses) {
		return businessHouseMapper.updateBatch(businessHouses);
	}

	/**
	 * 查询业务房屋集合
	 *
	 * @param businessHouse 请求参数
	 * @return /
	 */
	@Override
	public List<GwjInitBusinessHouse> getList(GwjInitBusinessHouse businessHouse) {
		return businessHouseMapper.selectList(businessHouse);
	}

	/**
	 * 根据楼栋标识码查询业务房屋集合
	 *
	 * @param selectFlag 房屋选择状态
	 * @param bizId      业务ID
	 * @return /
	 */
	@Override
	public List<GwjInitBusinessHouseDTO> getDtoList(String selectFlag, Long bizId) {
		GwjInitBusinessHouse businessHouse = new GwjInitBusinessHouse();
		businessHouse.setSelectFlag(selectFlag);
		businessHouse.setBizId(bizId);
		return businessHouseMapper.selectDtoList(businessHouse);
	}

	/**
	 * 根据楼栋标识码查询业务房屋集合
	 *
	 * @param selectFlag 房屋选择状态
	 * @param bizId      业务ID
	 * @return /
	 */
	@Override
	public List<GwjInitBusinessHouse> getList(String selectFlag, Long bizId) {
		GwjInitBusinessHouse businessHouse = new GwjInitBusinessHouse();
		businessHouse.setSelectFlag(selectFlag);
		businessHouse.setBizId(bizId);
		return businessHouseMapper.selectList(businessHouse);
	}

	/**
	 * 根据楼栋标识码和地区ID查询业务房屋集合
	 *
	 * @param buildCode 楼栋标识码
	 * @param bizId     业务ID
	 * @return /
	 */
	@Override
	public List<GwjInitBusinessHouse> getList(Long bizId, String buildCode) {
		GwjInitBusinessHouse businessHouse = new GwjInitBusinessHouse();
		businessHouse.setBuildCode(buildCode);
		businessHouse.setBizId(bizId);
		return businessHouseMapper.selectList(businessHouse);
	}

	/**
	 * 根据业务ID查询业务房屋集合
	 *
	 * @param bizId 业务ID
	 * @return /
	 */
	@Override
	public List<GwjInitBusinessHouse> getListByBizId(Long bizId) {
		GwjInitBusinessHouse businessHouse = new GwjInitBusinessHouse();
		businessHouse.setBizId(bizId);
		return businessHouseMapper.selectList(businessHouse);
	}

	/**
	 * 获取房屋集合
	 *
	 * @param buildCodes 楼栋标识码集合
	 * @param bizId      业务ID
	 * @return /
	 */
	@Override
	public List<GwjInitBusinessHouse> getList(String[] buildCodes, Long bizId) {
		return businessHouseMapper.selectListInCode(buildCodes, bizId);
	}

	/**
	 * 根据楼栋标识码删除房屋
	 *
	 * @param buildCodes 楼栋标识码
	 * @param bizId      业务ID
	 * @return /
	 */
	@Override
	public int deleteByBuildCodesAndBizId(List<String> buildCodes, Long bizId) {
		return businessHouseMapper.deleteByBuildCodesAndBizId(buildCodes, bizId);
	}

	/**
	 * 根据业务ID删除楼栋
	 *
	 * @param bizId 业务ID
	 * @return /
	 */
	@Override
	public int deleteByBizId(Long bizId) {
		return businessHouseMapper.deleteByBizId(bizId);
	}

	/**
	 * 取消选择
	 *
	 * @param bizHouseIds 业务房屋ID
	 */
	@Override
	public void cancelSelect(String bizHouseIds) {
		String[] ids = Convert.toStrArray(bizHouseIds.trim());
		List<GwjInitBusinessHouse> houses = new ArrayList<GwjInitBusinessHouse>();
		for (String id : ids) {
			GwjInitBusinessHouse house = new GwjInitBusinessHouse();
			house.setBizHouseId(Convert.toLong(id));
			house.setSelectFlag(GwjBusinessHouseStatus.CANCEL.getCode());
			houses.add(house);
		}
		businessHouseMapper.updateBatch(houses);
	}

	/**
	 * 加入选择
	 *
	 * @param bizHouseIds 业务房屋ID
	 */
	@Override
	public void confirmSelect(String bizHouseIds) {
		String[] ids = Convert.toStrArray(bizHouseIds.trim());
		List<GwjInitBusinessHouse> houses = new ArrayList<GwjInitBusinessHouse>();
		for (String id : ids) {
			GwjInitBusinessHouse house = new GwjInitBusinessHouse();
			house.setBizHouseId(Convert.toLong(id));
			house.setSelectFlag(GwjBusinessHouseStatus.CONFIRM.getCode());
			houses.add(house);
		}
		businessHouseMapper.updateBatch(houses);
	}
}
